<template>
    <div class="w640 donggu">
          <div class="dg-login-title">管理员登录</div>
          <div class="register">
            <div class="input-item">
              <input type="text" maxlength="10" placeholder="昵称" v-model="userName"/>
            </div>
            <div class="input-item">
              <input type="password" placeholder="密码" v-model="userPwd"/>
            </div>
            <div  v-if="!loging">
              <button class="sub-btn reg-btn"  @click="startLogin()" >登录</button>
            </div>
            <button v-if="loging" class="sub-btn reg-btn"  style="background: #ccc;color: #fff;">登录中...</button>
          </div>
    </div>
</template>

<script>
  import {Toast} from 'vue-ydui/dist/lib.rem/dialog';
    export default {
        name: "dg-login",
      data(){
          return{
            userName: '',
            userPwd: '',
            loging:false
          }
      },
      mounted(){
        document.title = "管理员登录";
      },
      methods:{
        startLogin() {
          if(!this.userName){
            Toast({mes:"用户名不能为空", timeout: 1000});
            return false;
          }
          if(!this.userPwd){
            Toast({mes:"密码不能为空", timeout: 1000});
            return false;
          }
          this.loging=true;
          this.$axios.post("/donggu/chekLogin", {
            userName: this.userName,
            userPwd: this.userPwd,
          }).then((res) => {
            let jsons = res.data;
            //存储到本地
            if(jsons.success){
              localStorage.setItem("dgUser",jsons.result.userName);
              setTimeout(()=>{
                this.$router.replace("/dgSelect");
              },1500)
            }
            else{
              this.loging=false;
            }

            Toast({mes: jsons.msg, timeout: 1000});
          }).catch(()=>{
            this.loging=false;
          })
        }
      }
    }
</script>
